package com.bw.module_motorcircle.ui.post.adapter

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bw.lib_common.widget.nine_pic_grid.Image
import com.bw.module_motorcircle.R
import com.bw.module_motorcircle.databinding.LayoutItemPostListBinding
import com.bw.module_motorcircle.model.PostItem
import com.bw.module_motorcircle.model.Topic

/**
 * @author ytx
 * @date 2025-09-25 16:16
 * @description
 */
class PostListAdapter(
    private val onImageClick:((Image,Int)->Unit)? = null,
    private val onTopicClick:((Topic)->Unit)? = null,
    private val onItemClick: ((PostItem, Int) -> Unit)? = null,
    private val onUserClick: ((PostItem) -> Unit)? = null,
    private val onShareClick: ((PostItem) -> Unit)? = null,
    private val onCommentsClick: ((PostItem) -> Unit)? = null,
    private val onLikeClick: ((PostItem, Int) -> Unit)? = null,
    private val onMoreActionClick: ((PostItem, Int) -> Unit)? = null,
    private val onLikeViewClick: ((PostItem, Int, View) -> Unit)? = null,
    private val onCollectionViewClick: ((PostItem, Int, View) -> Unit)? = null,
    private val onFollowViewClick: ((PostItem, Int, View) -> Unit)? = null,
    // 是否显示关系
    private val showRelationState: Boolean = true,
    //预加载处理
    private val onPreLoad:(()->Unit)? = null,//预加载回调
    private val preloadItemCount:Int = 0,//预加载偏移量，倒数第几个
    private var scrollState:Int  = RecyclerView.SCROLL_STATE_IDLE //滑动状态
):RecyclerView.Adapter<PostViewHolder>() {
    private val mData = mutableListOf<PostItem>()

    fun getPostItem(position: Int):PostItem{
        return mData[position]
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        recyclerView.addOnScrollListener(object :RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                //更新状态
                scrollState = newState
            }
        })
    }
    /**
     * 重置数据
     */
    fun setData(newData:List<PostItem>){
        val diff = DiffUtil.calculateDiff(PostsDiffCallBack(mData.toList(),newData))
        mData.clear()
        mData.addAll(newData)
        notifyDataSetChanged()
        diff.dispatchUpdatesTo(this)
    }

    /**
     * 追加数据
     */
    fun addData(newData: List<PostItem>){
        val size = mData.size
        mData.addAll(newData)
        notifyItemRangeInserted(size,newData.size)
    }


    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): PostViewHolder {
        val binding = LayoutItemPostListBinding.inflate(LayoutInflater.from(p0.context),p0,false)
        return PostViewHolder(
            mBinding = binding,
            onImageClick = onImageClick,
            onTopicClick = onTopicClick,
            onItemClick = onItemClick,
            onUserClick = onUserClick,
//            onImageClickWithThumbnail = onImageClickWithThumbnail,
            onShareClick = onShareClick,
            onCommentsClick = onCommentsClick,
            onLikeClick = onLikeClick,
            onMoreActionClick = onMoreActionClick,
            onLikeViewClick = onLikeViewClick,
            onCollectionViewClick = onCollectionViewClick,
            onFollowViewClick = onFollowViewClick
        )
    }

    override fun getItemCount(): Int {
       return mData.size
    }

    override fun onBindViewHolder(viewHolder: PostViewHolder, position: Int) {
        //判断是否需要预加载
        checkPreload(position)
        //databing展示数据
        mData[position].needShowFollowState = showRelationState //是否显示关系
        viewHolder.mBinding.holder = viewHolder
        viewHolder.mBinding.post = mData[position]

        //部分数据
        viewHolder.initView(mData[position])
    }

    /**
     * 判断是否需要预加载数据
     */
    private fun checkPreload(position: Int){
        if (onPreLoad != null && position == itemCount - preloadItemCount && scrollState != RecyclerView.SCROLL_STATE_IDLE){
            onPreLoad.invoke()
        }
    }

    /**
     * 局部增量刷新
     */
    override fun onBindViewHolder(
        holder: PostViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if (payloads.isNullOrEmpty()){
            onBindViewHolder(holder,position)
        }else{
            val  bundle  = payloads[0] as Bundle

            holder.mBinding.apply {
                if (bundle.containsKey("if_like")){
                    if(mData[position].if_like == 1){
                       ivLike.setImageResource(R.mipmap.icon_like_checked)
                    }else{
                       ivLike.setImageResource(R.mipmap.icon_like)
                    }
                }else if (bundle.containsKey("if_collect")){
                    if(mData[position].if_collect == 1){
                        ivCollection.setImageResource(R.mipmap.icon_collection_checked)
                    }else{
                        ivCollection.setImageResource(R.mipmap.icon_collection)
                    }
                }
            }
        }

    }
}